/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {
    var url = '/v1/manager/increase/score/list.json';

    var toolbar = '';

    var role = 1;

    loadList(url, toolbar, role);

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-status');

    $('.nav-tabs a').on('click', function () {
        if ($(this).attr('data-id') != role) {
            role = $(this).attr('data-id');
            if (role == 1) {
                $('#increase-user .row .col-xs-12').html('<table class="table table-hover data-table-user" data-height="600"></table>');
            } else {
                $('#increase-department .row .col-xs-12').html('<table class="table table-hover data-table-department" data-height="600"></table>');
            }
            loadList(url, toolbar, role);
        }
    });

    $('#normal-dialog').on('show.bs.modal', function (event) {
        var id = $(event.relatedTarget).data('id');
        var role = $(event.relatedTarget).data('role');
        var modal_title = $(this).find('.modal-title');
        var modal_body = $(this).find('.modal-body');
        var modal_footer = $(this).find('.modal-footer');
        sendAjax('/v1/manager/increase/score/detail.json', 'get', {id: id}, function (json) {
            console.log(json);
            var html = '<div class="form-horizontal">';
            html += '<div class="form-group">';
            html += '<label class="col-sm-2 control-label">文章标题：</label>';
            html += '<div class="col-sm-6">';
            html += '<input type="hidden" class="increase-id" value="'+json.body.id+'">';
            html += '<input type="hidden" class="department-id" value="'+json.body.department_id+'">';
            html += '<span class="increase-title">'+json.body.title+'</span>';
            html += '</div></div>';
            html += '<div class="form-group">';
            html += '<label class="col-sm-2 control-label">发布时间：</label>';
            html += '<div class="col-sm-6">';
            html += '<span class="increase-create-time">'+json.body.created_at+'</span>';
            html += '</div></div>';
            html += '<div class="form-group">';
            html += '<label class="col-sm-2 control-label">内容：</label>';
            html += '<div class="col-sm-6">';
            html += '<span class="increase-content">'+json.body.content+'</span>';
            html += '</div></div>';
            if (json.body.attachment != '') {
                html += '<div class="form-group">';
                html += '<label class="col-sm-2 control-label">附件：</label>';
                html += '<div class="col-sm-6">';
                html += '<span class="increase-attachment" title="点击查看"><a target="_blank" href="'+json.body.attachment+'"><img class="img-increase-attachment" src="'+json.body.attachment+'" width="120" height="160"></a></span>';
                html += '</div></div>';
            }
            if (role == 1) {
                modal_title.text('加分申请-详情');
                modal_footer.find('.dialog-btn-save').addClass('hidden');
            } else {
                var score = json.body.score > 0 ? json.body.score : '';
                modal_title.text('加分申请-打分');
                html += '<div class="form-group">';
                html += '<label class="col-sm-2 control-label">打分：</label>';
                html += '<div class="col-sm-6">';
                html += '<input type="text" class="form-control increase-score" value="'+score+'">';
                html += '</div></div>';
            }
            html += '</div>';
            modal_body.html(html);
        })
    }).on('hide.bs.modal', function () {
        $(this).find('.modal-title').text('普通弹窗');
        $(this).find('.modal-body').html('');
        $(this).find('.modal-footer .dialog-btn-save').removeClass('hidden');
    });

    $('.dialog-btn-save').on('click', function () {
        var data = {
            id : $('.increase-id').val(),
            score : $('.increase-score').val()
        };

        if (checkForm(data)) {
            saveData('/v1/manager/save/department/increase', {params:data}, '', '打分完成');
        }
    });

    $(document).on('click', '.btn-delete', function () {
        var id = $(this).attr('data-id');
        deleteData('/v1/manager/delete/increase/score', id);
    });

});

function loadList(url, toolbar, role) {
    var selector = '.data-table-department';
    var name = 'name';
    var title = '党支部名称';
    if (role == 1) {
        selector = '.data-table-user';
        name = 'realname';
        title = '党员姓名';
    }
    $(selector).bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: toolbar,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        search: true,
        sortable: false,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        queryParams : function (params) {
            params.status = $('.search-key').val();
            params.role = role;
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: '序号'
        }, {
            field: 'title',
            title: '文章标题'
        }, {
            field: name,
            title: title
        }, {
            field: 'created_at',
            title: '时间'
        }, {
            field: 'score',
            title: '分数'
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-sm btn-info" href="#" data-id="'+row.id+'" data-role="'+role+'" data-toggle="modal" data-target="#normal-dialog"><i class="fa fa-edit"></i>详情</a>';
                action += '<a class="btn btn-sm btn-danger btn-delete" href="#" data-id="'+row.id+'"><i class="fa fa-close">删除</a>';
                return action;
            }
        }]
    });
}

function refreshList() {
    $('.data-table').bootstrapTable('refresh');
}

function checkForm(data) {
    if (data.score == '') {
        alertTip('分数不能为空');
        return false;
    }
    return true;
}